//
//  HMTabBar.m
//  swift-新浪微博OC版-8-29
//
//  Created by 赵玉忠 on 16/8/30.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "HMTabBar.h"
@interface HMTabBar()
@property (nonatomic,strong) UIButton *button;

@end
@implementation HMTabBar

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setupUI];
}
-(void)setupUI{
    self.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    [self addSubview:self.button];
}
-(void)buttonClick{
    if (self.block) {
        self.block();
    }
}
-(UIButton *)button{
    if (_button == nil) {
        UIButton *btn = [[UIButton alloc]init];
        _button = btn;
        [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [btn sizeToFit];
    }
    return _button;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    _button.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    CGFloat itemW = [UIScreen mainScreen].bounds.size.width / 5;
    int index = 0;
    
    for (UIView *v in self.subviews) {
        if ([v isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            CGFloat itemX = itemW * index;
            CGRect frame = v.frame;
            frame.size.width = itemW;
            frame.origin.x = itemX;
            v.frame = frame;
            
            index ++;
            if (index == 2) {
                index++;
            }
        }
    }
    
}
@end
